import 'package:file_transfer/manager/webrtc_manager.dart';
import 'package:file_transfer/page/home_page.dart';
import 'package:file_transfer/route/simple_route.dart';
import 'package:file_transfer/utils/toast_util.dart';
import 'package:flutter/material.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {

  final _textController= TextEditingController();

  _login(BuildContext context){
    debugPrint("login");
    final text = _textController.text;
    if(text.isEmpty){
      debugPrint("字符串为空");
      return;
    }

    final webrtcManager = WebrtcManager.instance(context);
    final res = webrtcManager.login(text);
    if(res){
      SimpleRoute.instance().toHomePage(context);
    }else{
      debugPrint("登录失败");
      ToastUtil.show("登录失败");
    }

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset:false, // 禁止页面上移
      body: Center(
        child: Container(
          padding: const EdgeInsets.all(10.0),
          margin: const EdgeInsets.all(20.0),
          height: 200,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(15.0),
            color: Colors.blueAccent
          ),
          child: Column(
            children: [
              TextField(
                style: const TextStyle(
                  color: Colors.white,
                  fontWeight: FontWeight.bold,
                  fontSize: 20
                ),
                controller: _textController,
                minLines: 1,
                maxLines: 1,
                decoration: const InputDecoration(
                  hintText: "请输入登录用户名",
                  border: InputBorder.none,
                  // 设置边框为无
                  focusedBorder: InputBorder.none,
                  // 设置聚焦时的边框为无
                  enabledBorder: InputBorder.none,
                  // 设置启用时的边框为无
                  errorBorder: InputBorder.none,
                  // 设置错误时的边框为无
                  disabledBorder: InputBorder.none, // 设置禁用时的边框为无
                ),
              ),
              InkWell(
                onTap: ()=>_login(context),
                child: Container(
                  height: 50,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(6.0)
                  ),
                  child: const Center(child: Text("登录",style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold
                  ),)),
                ),
              ),
              const SizedBox(height: 15),
              InkWell(
                onTap: ()=>SimpleRoute.instance().toBack(context),
                child: Container(
                  height: 50,
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(6.0)
                  ),
                  child: const Center(child: Text("返回上一级",style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.bold
                  ),)),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
